import frappe
from frappe.model.document import Document
from frappe.model.docstatus import DocStatus
from frappe import _

from frappe import new_doc
from frappe import session


def cs(doc, method=None):
    communication = frappe.new_doc("Communication")
    communication.communication_type = "Notification"
    communication.communication_medium = "Event"
    communication.subject = _("流程追踪单")

    msg = _(f"流程追踪单:创建成功! 点击 <a href=''>这里</a> 查看")
    communication.content = msg
    # 获取当前登录用户并添加到收件人列表
    communication.recipients = f"{session.user},ywy1@qq.com,业务员1"
    communication.insert()


def 物料需求提交(doc, method=None):
    # 测试能否拿到前端消息
    amtf = frappe.get_request_header("amtf")
    print(f"frappe 👉 {amtf}")

    print(f"doc 👉 {doc}")
    # 流程追踪ing = doc.流程追踪ing
    msg = _(f"流程追踪ing 👉 {amtf}")
    frappe.msgprint(msg, "流程追踪单消息")


def 销售单提交(doc, method=None):
    print(f"销售单提交 ing👇")
    # return

    doc1 = frappe.new_doc("lczz")
    doc1.销售订单 = doc.name
    doc1.当前负责角色 = "Sales User"
    frappe.session.自动创建lczzing = True
    doc1.insert()

    # 创建消息，收不到通知，待续
    # communication = frappe.new_doc("Communication")
    # communication.communication_type = "Communication"
    # communication.communication_medium = "Event"
    # communication.subject = _("流程追踪单")
    # communication.content = msg
    # # 获取当前登录用户并添加到收件人列表
    # communication.recipients = f"{session.user},ywy1@qq.com,业务员1"
    # communication.insert()

    # link = f"/app/lczz/{doc1.name}"
    # # link = f"/app/lczz"
    # msg = _(f"流程追踪单:{doc1.name} 创建成功! 点击 <a href='{link}'>这里</a> 查看")

    # msg=doc1.流程提醒信息
    # frappe.msgprint(msg,'流程追踪单消息')


def 采购单提交(doc, method=None):
    # msg = _(f"流程追踪单id:{doc.追踪id} ")
    # frappe.msgprint(msg)
    pass


def on_sales_order_submit(doc, method):
    # 创建一个新的流程追踪单记录
    process_tracking_doc = new_doc("Sales Order Process Tracking")
    # 填充销售订单的主要信息
    process_tracking_doc.sales_order_number = doc.name
    process_tracking_doc.customer_name = doc.customer_name
    process_tracking_doc.total_amount = doc.grand_total
    # 设置状态为"待收首款"
    process_tracking_doc.status = "待收首款"
    # 保存流程追踪单记录
    process_tracking_doc.save()
